//
//  CalendarUtils.m
//  PhotoFrame
//
//  Created by William on 11-5-3.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalendarUtils.h"


@implementation CalendarUtils

- (CalendarObjectDTO *) getCalendarInfo {
	NSDate *date = [NSDate date];
	NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

	NSInteger unitFlags = NSYearCalendarUnit |      
	NSMonthCalendarUnit |     
	NSDayCalendarUnit |      
	NSWeekdayCalendarUnit |      
	NSHourCalendarUnit |     
	NSMinuteCalendarUnit |     
	NSSecondCalendarUnit; 
	
	NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    
	CalendarObjectDTO *calendarObject = [[CalendarObjectDTO alloc] init];
	calendarObject.year = [components year];
	calendarObject.month = [components month];
	calendarObject.day = [components day];
	calendarObject.hour = [components hour];
	calendarObject.minute = [components minute];
	calendarObject.second = [components second];
	
	NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
							  inUnit:NSMonthCalendarUnit
							 forDate:[calendar dateFromComponents:components]];
	
	calendarObject.numOfDay = range.length;
	
	[calendar release];
	calendar = nil;
	
	return calendarObject;
}

- (int) getWeekdayWithYear:(int) year month:(int) month day:(int) day {
	NSDateComponents *comps = [[NSDateComponents alloc] init];
	[comps setDay:day];
	[comps setMonth:month];
	[comps setYear:year];
	NSCalendar *gregorian = [[NSCalendar alloc]
							 initWithCalendarIdentifier:NSGregorianCalendar];
	NSDate *date = [gregorian dateFromComponents:comps];
	[comps release];
	NSDateComponents *weekdayComponents =
    [gregorian components:NSWeekdayCalendarUnit fromDate:date];
	NSInteger weekday = [weekdayComponents weekday];
	[gregorian release];
	
	return weekday;
}
@end
